package org.example.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dto.DailyUserResponseDTO;
import org.example.dto.UserVO;
import org.example.entity.Users;
import org.example.dto.UserStatisticsDTO;
import org.example.mapper.UsersMapper;
import org.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @version 1.0
 * @Description
 * @date 2025/7/21 15:14
 */
@Service
public class UsersServiceImp extends ServiceImpl<UsersMapper, Users> implements UsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<Users> getAllUsers() {
        return usersMapper.getAllUsers();
    }

    @Override
    public Users getUserByOpenid(String openid) {
        return usersMapper.getUserByOpenid(openid);
    }

    @Override
    public Users saveOrUpdateUser(String openid, String unionid, String nickname, String avatar) {
        // 先查询用户是否存在
        Users existingUser = getUserByOpenid(openid);
        
        if (existingUser != null) {
            // 用户已存在，更新信息
            existingUser.setNickname(nickname);
            existingUser.setAvatar(avatar);
            existingUser.setUserid(unionid);
            existingUser.setUpdatedTime(new Timestamp(System.currentTimeMillis()));
            // 使用自定义的更新方法
            usersMapper.updateUserInfo(existingUser);
            return existingUser;
        } else {
            // 用户不存在，创建新用户
            Users newUser = new Users();
            newUser.setOpenid(openid);
            newUser.setUserid(unionid);
            newUser.setNickname(nickname);
            newUser.setAvatar(avatar);
            newUser.setBalance(0); // 初始积分为0
            newUser.setMemberType(0); // 初始为普通用户
            newUser.setStatus(1); // 状态为正常
            newUser.setCreatedTime(new Timestamp(System.currentTimeMillis()));
            newUser.setUpdatedTime(new Timestamp(System.currentTimeMillis()));
            
            usersMapper.saveUser(newUser);
            return newUser;
        }
    }

    @Override
    public String updateUserInfo(Users users) {
        return usersMapper.updateUserInfo(users)>0?"修改成功":"修改失败";
    }

    @Override
    public UserStatisticsDTO getUserStatistics() {
        return usersMapper.countUserStatistics();
    }

    @Override
    public List<UserVO> getDailyActiveUsers(LocalDate date) {
        List<Users> users = usersMapper.selectUsersByDate(date);
        if (users == null) {
            return Collections.emptyList();
        }
        return users.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public int getDailyActiveUserCount(LocalDate date) {
        return usersMapper.countByLastLoginDate(date);
    }

    private UserVO convertToVO(Users user) {
        if (user == null) {
            return null; // 或者 return new UserVO() 根据业务需求
        }
        return new UserVO(
                user.getId() != null ? Long.valueOf(user.getId().toString()) : null,
                user.getUserid() != null ? user.getUserid().toString() : null,
                user.getOpenid(),
                user.getNickname()
        );
    }
}
